package com.smcaiot.filesync.starter.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.smcaiot.filesync.starter.app.FileSyncProperties;
import com.smcaiot.filesync.starter.app.HandlerContext;
import com.smcaiot.filesync.starter.entity.Base64Param;
import com.smcaiot.filesync.starter.entity.FindNotPullListParams;
import com.smcaiot.filesync.starter.entity.FinishPullParams;
import com.smcaiot.filesync.starter.entity.ReceiveParams;
import com.smcaiot.filesync.starter.handler.FileHandler;
import com.smcaiot.filesync.starter.handler.SyncFinishHandler;
import com.smcaiot.filesync.starter.handler.impl.AbstractPullFileHandler;
import com.smcaiot.filesync.starter.service.RemoteService;
import com.smcaiot.filesync.starter.util.ReflectTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: cz
 * @Date: 2023/6/29
 * @Description:
 */
@Service
@Slf4j
public class RemoteServiceImpl implements RemoteService {

    @Autowired
    private FileSyncProperties fileSyncProp;
    @Autowired
    private FileHandler fileHandler;
    @Autowired
    private HandlerContext handlerContext;

    @Override
    public String receiveFile(ReceiveParams params) {
        Base64Param base64Param = new Base64Param().setBase64(params.getBase64()).setOriginName(true)
                .setFilePath(fileSyncProp.getBaseDir() + "/" + params.getFileName());
        String filePath = fileHandler.uploadBase64(base64Param);
        if (StrUtil.isBlank(filePath)) {
            return null;
        }
        BeanUtil.setProperty(params.getEntity(), params.getPathFieldName(), filePath);
        SyncFinishHandler handler = handlerContext.getHandler(params.getHandlerType(), params.getEntityClassName());
        Class genericClz = ReflectTool.getGenericParameterClass(handler.getClass());
        Object entity = JSONUtil.toBean(JSONUtil.toJsonStr(params.getEntity()), genericClz);
        return handler.finish(Collections.singletonList(entity)) ? filePath : null;
    }

    @Override
    public List findNotPullList(FindNotPullListParams params) {
        SyncFinishHandler handler = handlerContext.getHandler(params.getHandlerType(), params.getEntityClassName());
        List list = ((AbstractPullFileHandler) handler).findNotSyncList();
        return CollUtil.isEmpty(list) ? new ArrayList() : list;
    }

    @Override
    public boolean finishPull(FinishPullParams params) {
        SyncFinishHandler handler = handlerContext.getHandler(params.getHandlerType(), params.getEntityClassName());
        Class genericClz = ReflectTool.getGenericParameterClass(handler.getClass());
        List list = JSONUtil.toList(JSONUtil.parseArray(params.getList()), genericClz);
        return handler.finish(list);
    }

}
